package com.lin.maker.template;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.lin.maker.template.Enum.FileFilterRangeEnum;
import com.lin.maker.template.Enum.FileFilterRuleEnum;
import com.lin.maker.template.model.FileFilterConfig;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

public class FileFilter {
    /**
     * 对单个文件进行过滤的规则
     * @param fileFilterConfigList
     * @param file
     * @return
     */
    private static boolean doSingleFileFilter(List<FileFilterConfig> fileFilterConfigList, File file){
        // 获取文件信息
        String fileName  = file.getName();
        String fileContent = FileUtil.readUtf8String(file);
        // 最后的结果
        boolean result = true;

        if(CollectionUtil.isEmpty(fileFilterConfigList)){
            return result;
        }

        for (FileFilterConfig fileFilterConfig : fileFilterConfigList) {
            String range = fileFilterConfig.getRange();
            String rule = fileFilterConfig.getRule();
            String value = fileFilterConfig.getValue();

            FileFilterRangeEnum fileFilterRangeEnum = FileFilterRangeEnum.getEnumByValue(range);
            if(fileFilterRangeEnum == null){
                continue;
            }
            // fileName 就是对文件名进行过滤， fileContent 就是对文件内容进行过滤
            // 我们这里用一个这个公共的content 进行收敛
            String content = fileName;
            switch (fileFilterRangeEnum){
                case FILE_NAME -> {
                    content = fileName;
                    break;
                }
                case FILE_CONTENT -> {
                    content = fileContent;
                    break;
                }
            }
            FileFilterRuleEnum fileFilterRuleEnum = FileFilterRuleEnum.getEnumByValue(rule);
            if(fileFilterRuleEnum == null){
                continue;
            }
            switch (fileFilterRuleEnum) {
                case CONTAINS -> {
                    result = content.contains(value);
                    break;
                }
                case STARTS_WITH -> {
                    result = content.startsWith(value);
                    break;
                }
                case ENDS_WITH -> {
                    result = content.endsWith(value);
                    break;
                }
                case REGEX -> {
                    result = content.matches(value);
                    break;
                }
                case EQUALS -> {
                    result = content.equals(value);
                    break;
                }
            }
            if(!result) return result;
        }
        return result;
    }

    /**
     * 对某个文件列表进行过滤返回文件列表
     * @param filePath
     * @param fileFilterConfigList
     * @return
     */
    public static List<File> doFilter(String filePath, List<FileFilterConfig> fileFilterConfigList) {
        List<File> fileList = FileUtil.loopFiles(filePath);
        return fileList.stream().
                filter(file -> doSingleFileFilter(fileFilterConfigList,file)).
                collect(Collectors.toList());
    }
}
